package gd.hq.yolov5.comunicate;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.util.UUID;

class BleBroadCastRevciver extends BroadcastReceiver {
    private String mac;
    private UUID uuid;
    BluetoothDevice b_device = null;

    private BluetoothActivity bluetoothActivity = null;

    public BleBroadCastRevciver(String mac,BluetoothActivity activity) {
        this.mac = mac;
        this.bluetoothActivity = activity;
    }

    public String getMac() {
        return mac;
    }

    public UUID getUuid() {
        return uuid;
    }

    public BluetoothDevice getB_device() {
        return b_device;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
            case BluetoothDevice.ACTION_FOUND:

                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if(device!=null&&device.getName()!=null&&device.getName().equals("HC-06")) {
                    mac = device.getAddress();
                    Log.d("TAG", "找到蓝牙");
                    Log.d("device", mac+"  "+device.getUuids().length+"   "+device.getUuids()[0].getUuid().toString()+"  "+device.getUuids()[0].describeContents());
                    uuid = device.getUuids()[0].getUuid();
                    b_device = device;
                    bluetoothActivity.setText("找到蓝牙"+device.getName()+"  "+mac);
                }
                break;
            case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                switch (device.getBondState()) {
                    case BluetoothDevice.BOND_NONE:
                        Log.d("TAG", "取消配对");

                        break;
                    case BluetoothDevice.BOND_BONDING:
                        Log.d("TAG", "配对中");
                        break;
                    case BluetoothDevice.BOND_BONDED:
                        Log.d("TAG", "配对成功");
                        break;
                }
                break;
            case BluetoothDevice.ACTION_ACL_CONNECTED:
                Log.d("TAG", "连接");
                break;
            case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                Log.d("TAG", "断开");
                break;
            default:
                break;
        }
    }
}